import threading
import asyncio


async def hello():
    print('hello world! (%s)' % threading.currentThread())
    await asyncio.sleep(3)
    print('hello world, again! (%s)' % threading.currentThread())


async def wget(host):
    print('wget %s...' % host)
    connection = asyncio.open_connection(host, 80)
    reader, writer = await connection
    header = 'Get / HTTP/1.0\r\nHost: %s\r\n\r\n' % host
    writer.write(header.encode('utf-8'))
    await writer.drain()
    while True:
        line = await reader.readline()
        await asyncio.sleep(2)
        if line == b'\r\n':
            break
        print('%s header > %s' % (host, line.decode('utf-8').rstrip()))
    # ignore the body, close the socket
    writer.close()


loop = asyncio.get_event_loop()
# tasks=[hello(),hello()]
tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
